//注意:引入import 'package:flutter/foundation.dart';会导致直接运行dart报错,提示无法获得ui库那个问题

/*
打印日志
 */


abstract class XLogUtils{

  static const int _limitLength = 500;
  static bool isEnable=true;
  static bool _isPrintOtherTag=false;//是否打印其他自定义tag的内容

  //直接调用打印log
  static void printLog(
      String? str,
      {
        String? tag,
        Exception? error,
        StackTrace? stackTrace,
        bool isPrintAll=false,
      }
      ) {
    //log('xsdk: $str');//只在run中打印,在logcat中不打印

    if(!isEnable){
      return;
    }

    if(error!=null){
      if (tag == null) {
        print('xsdk: error= $error, stackTrace=$stackTrace');
      } else if(_isPrintOtherTag){
        print('$tag: error= $error, stackTrace=$stackTrace');
      }
      return;
    }

    if(str==null||str.isEmpty){
      return;
    }

    if(isPrintAll){
      _printParagraphs(str,tag: tag);
      return;
    }

    //实测:打印日志过长,会导致flutter耗尽堆空间报错,因此不能在内存保持记录,
    if(str.length>_limitLength*10){
      str=str.substring(0,_limitLength*10);
    }

    //支持分段打印
    _printParagraphs(str,tag: tag);

  }


  //分段打印(递归)
  static void _printParagraphs(String? totalText,{String? tag}){

    if(totalText!=null && totalText.length>_limitLength){
      //分段打印
      String text1=totalText.substring(0,_limitLength);
      String text2=totalText.substring(_limitLength);

      if (tag == null) {
        print('xsdk: $text1');
      } else if(_isPrintOtherTag){
        print('$tag: $text1');
      }
      //
      _printParagraphs(text2);

    }else{

      if (tag == null) {
        print('xsdk: $totalText');
      } else if(_isPrintOtherTag) {
        print('$tag: $totalText');
      }

    }


  }



}
